#include "at24c02.h"
#include "i2c.h"
#include "lcd1602.h"
#include "task.h"

#if AT24C02_TASK

/* define your data start */
static volatile uint8_t at24_count_demo = 0;
/* define your data end */

/**
 * @brief at24c02_write_byte
 *
 * @param write_data_addr
 * @param write_data
 */
void at24c02_write_byte(uint8_t write_data_addr, uint8_t write_data)
{
    uint8_t write_buff[2];
    write_buff[0] = write_data_addr;
    write_buff[1] = write_data;

    master_i2c_write_byte_to_device(AT24C02_ADDR, write_buff, 2);
}

/**
 * @brief at24c02_read_data
 *
 * @param read_data_addr
 * @param *read_data
 */
void at24c02_read_data(uint8_t read_data_addr, uint8_t *read_data)
{
    master_i2c_write_byte_to_device(AT24C02_ADDR, &read_data_addr, 1);
    master_i2c_read_byte_from_device(AT24C02_ADDR, read_data);
}

/**
 * @brief at24_wr_rd_demo_init
 *
 */
void at24_wr_rd_demo_init(void)
{
    at24c02_read_data(0x01, &at24_count_demo); /* init read at24c02 data */
}

/**
 * @brief at24_wr_rd_demo
 *
 */
void at24_wr_rd_demo(void)
{
    at24c02_write_byte(0x01, at24_count_demo++);
    at24c02_read_data(0x01, &at24_count_demo);

    lcd1602_display_str(0, 0, "AT24C02-0x01-");
    lcd1602_display_one_num(0, 14, at24_count_demo / 100);
    lcd1602_display_one_num(0, 15, (at24_count_demo % 100) / 10);
    lcd1602_display_one_num(0, 16, at24_count_demo % 10);

#if AT24C02_DEBUG
    printf("read: [%bu]->%bu\r\n", 0x01, at24_count_demo);
#endif
}

#endif